package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import se.perp.c4.control.iface.ISpecialCardListener;
import se.perp.c4.control.iface.ISpecialCardWarningOwner;

public abstract class SpecialCard extends JComponent implements MouseListener {

    private BufferedImage enabled,  disabled;
    private ImagePanel enabledComponent,  disabledComponent;
    private boolean isEnabled = true;
    private ISpecialCardListener listener;
    private boolean isResponsive = true;
    private boolean hasBeenPlayed = false;
    protected ISpecialCardWarningOwner warningOwner = null;
    private String enabledToolTip, warning;

    public SpecialCard(BufferedImage baseImage, String enabledToolTip,
            String disabledToolTip, String warning, ISpecialCardListener listener) {
        this.listener = listener;
        this.warning = warning;
        enabled = baseImage;

        disabled = new BufferedImage(enabled.getWidth(), enabled.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) disabled.getGraphics();
        g.drawImage(enabled, 0, 0, null);

        g.setColor(new Color(0, 0, 0, 192));
        g.fillRect(0, 0, disabled.getWidth(), disabled.getHeight());

        Color borderColor = new Color(200, 200, 200);

        setLayout(null);
        enabledComponent = new ImagePanel(enabled);
        enabledComponent.setToolTipText(enabledToolTip);
        enabledComponent.setLocation(0, 0);
        enabledComponent.addMouseListener(this);
        enabledComponent.setBorder(BorderFactory.createLineBorder(borderColor, 1));
        enabledComponent.setHoverSensitive(true);
        add(enabledComponent);

        this.enabledToolTip = enabledToolTip;

        disabledComponent = new ImagePanel(disabled);
        disabledComponent.setToolTipText(disabledToolTip);
        disabledComponent.setLocation(0, 0);
        disabledComponent.setBorder(BorderFactory.createLineBorder(borderColor, 1));
        disabledComponent.setHoverSensitive(false);
        add(disabledComponent);

        enabledComponent.setVisible(isEnabled);
        disabledComponent.setVisible(!isEnabled);

        setSize(enabledComponent.getSize());
        setPreferredSize(getSize());

        addMouseListener(this);

    }

    public void setWarningOwner(ISpecialCardWarningOwner l) {
        this.warningOwner = l;
    }

    @Override
    public void setEnabled(boolean b) {
        isEnabled = b;
        enabledComponent.setVisible(b);
        disabledComponent.setVisible(!b);
    }

    public boolean hasBeenPlayed() {
        return hasBeenPlayed;
    }

    @Override
    public boolean isEnabled() {
        return isEnabled;
    }

    public void mousePressed(MouseEvent e) {
        if (isResponsive && isEnabled) {

            boolean ok = true;
            if (warningOwner.getWarnAboutSpecialCards()) {
                int reply = MessageBox.yesNo(null, warning, "Special card", null, false);
                ok = (reply == JOptionPane.YES_OPTION);
            }
            if (ok) {
                hasBeenPlayed = true;
                listener.cardClicked(this);
            }
        }
    }

    public boolean isResponsive() {
        return isResponsive;
    }

    public void setResponsive(boolean isResponsive) {
        this.isResponsive = isResponsive;
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    String getEnabledTooltipText() {
        return enabledToolTip;
    }
}
